<?php
header("Content-type: text/html; charset=utf-8");
set_time_limit(0);
$ch_1 = curl_init(); 
@$page=$_GET['page']?$_GET['page']:1;//当前音乐分页
@$pagesize=$_GET['pagesize']?$_GET['pagesize']:10;//当前显示多少个
@$keyword=$_GET['keyword']?$_GET['keyword']:'你说的';//需要搜索的音乐
if(empty($page)){
	$page=1;
}
if(empty($pagesize)){
	$pagesize=30;
}
if(empty($keyword)){
	exit('ERR_KEYWORD_EMPTY');
}
$temp_get='format=json&keyword='.$keyword.'&page='.$page.'&pagesize='.$pagesize;
curl_setopt ($ch_1, CURLOPT_URL,"http://mobilecdn.kugou.com/api/v3/search/song?".$temp_get);
curl_setopt ($ch_1, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch_1, CURLOPT_CONNECTTIMEOUT, 10); 
$query_array=json_decode(curl_exec($ch_1),true);
curl_close($ch_1); 
$__data=array();
$__i=0;
foreach ($query_array['data']['info'] as $__id => $__d) {
	$__i++;
	foreach ($query_array['data']['info'][$__id] as $__n => $__v) {
		if($__n=='filename'){
			@$__data[$__i]['songdata']=$__v;
		}
		if($__n=='singername'){
//匹配歌手头像
			$ch_2 = curl_init(); 
			curl_setopt ($ch_2, CURLOPT_URL,"http://m.kugou.com/app/i/getSingerHead_new.php?singerName=".$__v);
			curl_setopt ($ch_2, CURLOPT_RETURNTRANSFER, 1); 
			curl_setopt ($ch_2, CURLOPT_CONNECTTIMEOUT, 10); 
			$head_data=json_decode(curl_exec($ch_2),true);
			curl_close($ch_2);
			@$__data[$__i]['head']=$head_data['url'];
		}
		if($__n=='hash'){
//根据hash搜索播放链接
			$ch_3 = curl_init();
			curl_setopt ($ch_3, CURLOPT_URL,"http://m.kugou.com/app/i/getSongInfo.php?cmd=playInfo&hash=".$__v);
			curl_setopt ($ch_3, CURLOPT_RETURNTRANSFER, 1); 
			curl_setopt ($ch_3, CURLOPT_CONNECTTIMEOUT, 10); 
			$play_data=json_decode(curl_exec($ch_3),true);
			curl_close($ch_3); 
			@$__data[$__i]['url']=$play_data['url'];
			//匹配歌词
			$ch_4 = curl_init(); 
			$temp_get='cmd=100&keyword='.$play_data['fileName'].'&hash='.$play_data['hash'].'&timelength='.$play_data['timeLength'].'000&d=0.'.rand(1000,9999);
			curl_setopt ($ch_4, CURLOPT_URL,"http://m.kugou.com/app/i/krc.php?".$temp_get);
			curl_setopt ($ch_4, CURLOPT_RETURNTRANSFER, 1); 
			curl_setopt ($ch_4, CURLOPT_CONNECTTIMEOUT, 10); 
			@$__data[$__i]['lrc']= curl_exec($ch_4); 
			curl_close($ch_4);
		}
	}
}
#print_r($__data);
var_dump($__data);
//echo json_encode($__data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES)
#不转义斜杠和中文字符（php_5.5+）
?>